package com.course02.order_backend.security.LXY;

import cn.hutool.json.JSONUtil;
import com.course02.order_backend.dto.LXY.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import java.io.IOException;

/**
 * 当认证失败或者未认证的账号访问私有资源时怎么处理（匿名处理方法）
 * @author wozhe
 */
@Component
public class LoginUnAuthenticationEntryPointHandler implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        Result error=Result.error("用户未登录或者登录已经过期，请重新登录（lxy自定义）");
        String json= JSONUtil.toJsonStr(error);
        response.getWriter().print(json);
    }

}
